<?php 
	include_once('Common.php');
	class User extends DModel{
		protected $_name = 'user';
		public function checkAndInsert($data){
			$sql = $this -> select()
			->where("email = ?",$data['email'])
			;
			$check = $this -> fetchRow($sql);
			if(count($check) > 0){
				return false;
			}else{
				return $this -> insert($data);
			}
		}
		public function get($data){
			$sql = $this -> select();
			foreach($data as $key => $val){
				$sql -> where($key . " = ?",$val);
			}
			return $this->fetchRow($sql);
		}
		
		public function search_confirm($list){
			$sql = $this -> select();
			if(!empty($list)){
				$sql = $sql -> where("id IN (?)",$list);
			}else{
				$sql = $sql -> where("id != ?",$user->id);
			}
			return $this->fetchAll($sql);
		}
		public function getuserbyid($id,$select = array(),$option = array()){
			$sql = $this -> select();
			if(!empty($select)){
				$sql = $sql -> from('user',$select);
			}
			if(!empty($option)){
				foreach($option as $key => $val){
					if(is_array($val)){
						$sql = $sql -> where($key . " IN (?) ", $val);		
					}else{
						$sql = $sql -> where($key . " = ? ", $val);	
					}
				}
			}
			$sql = $sql -> where('id = ?', $id); 
			return $this->fetchRow($sql);
		}
		
		public function validate_user($email, $input_password){
			try{
				$sql = $this -> select()
				->where("email = ?",$email)
				;
				$user = $this -> fetchRow($sql);
				$salt = $user['password_salt'];
				$db_password_hashed = $user['encrypted_password'];
				$input_password_hashed = digest($input_password, 5, $salt);
				if($db_password_hashed == $input_password_hashed)
					return $user;
				else{
					return false;	
				}
				
			}
			catch(Exception $e){
				return false;
			}
		}
	}
?>